package com.itheima.controller;


import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.Link;
import com.itheima.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 友情链接表 前端控制器
 * </p>
 *
 * @author chenzhi
 * @since 2020-08-05
 */
@Controller
@RequestMapping("/link")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class LinkController {

    @Autowired
    private LinkService linkService;

    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Link link) {
        try {
            linkService.add(link);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "添加数据失败");
        }
        return new Result("success", "添加数据成功");
    }

    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody Link link) {
        try {
            linkService.delete(link);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "删除数据失败");
        }
        return new Result("success", "删除数据成功");
    }

    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody Link link) {
        try {
            linkService.edit(link);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "修改数据失败");
        }
        return new Result("success", "修改数据成功");
    }

    @PostMapping("/getList")
    @ResponseBody
    public Result getList(@RequestBody Map<String, Object> searchMap) {
        Page<Link> pageList=linkService.getList(searchMap);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setTotal((int) pageList.getTotal());
        result.setRecords(pageList.getResult());
        return new Result("success", result);
    }


    @PostMapping("/stick")
    @ResponseBody//置顶操作
    public Result stick(@RequestBody Link link) {
        try {
            linkService.stick(link);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "操作失败");
        }
        return new Result("success", "操作成功");
    }
}

